package com.zcl.controller2;

import com.zcl.mapper2.ScoreMapper;
import com.zcl.pojo2.Classes;
import com.zcl.pojo2.PageResult;
import com.zcl.pojo2.Student;
import com.zcl.pojo2.score;
import com.zcl.service2.ScoreService;
import com.zcl.service2.ClassesService;
import com.zcl.service2.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/student2")
public class controller {
    //自动注入两个mapper文件
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private ScoreService scoreService;

    @RequestMapping("/list")
    public String list(Model model){
        //查询所有学生
        List<Student> students =studentService.findAll();
        //放入model容器中
        model.addAttribute("student2",students);
        return "student2/list";
    }
    //分页查询
    @RequestMapping("/findByPage")
    public String findByPage(@RequestParam(required = false,defaultValue = "1") int page,
                             @RequestParam(required = false,defaultValue = "5") int pageSize,
                             Model model){
        PageResult<Student> pr =studentService.findByPage(page,pageSize);
        //分页结果放入model
        model.addAttribute("pr",pr);
        return "student2/list";
    }
    //分页查询带条件
    @RequestMapping("/search")
    public String search(@RequestParam(required = false,defaultValue = "1") int page,
                         @RequestParam(required = false,defaultValue = "5") int pageSize,
                         Model model,
                         Student student){
        //根据传入的学生条件查询
        PageResult<Student> pr =studentService.search(page,pageSize,student);
        List<Classes> classes= classesService.findAll();
        List<score> score =scoreService.findAll();
        //分页结果放入model
        model.addAttribute("pr",pr);
        model.addAttribute("score",score);
        model.addAttribute("student",student);
        model.addAttribute("classes",classes);
        return "student2/list";
    }
    //添加学生
    @RequestMapping("/add")
    public String add(Student student){
        studentService.insert(student);
        return "redirect:/student2/search.do";
    }
    //编辑学生的界面
    @RequestMapping("/edit")
    public String edit(Model model,String sid){
//        查询所有班级
         List<Classes> classes = classesService.findAll();
//         根据ID查询学生对象
        Student student= studentService.findById(sid);
        model.addAttribute("classes",classes);
        model.addAttribute("student",student);
        return "student2/edit";
    }
    //修改学生
    @RequestMapping("/update")
    public String update(Student student){
        //修改学生
        studentService.update(student);
        //返回视图,redirect重定向跳转
        return "redirect:/student2/search.do";
    }
    //删除学生
    @RequestMapping("/delete")
    public String delete(Student student){
        //调用删除方法
        studentService.delete(student);
        //返回视图
        return "redirect:/student2/search.do";
    }
}
